package com.ma.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ma.auth.service.SysUserService;
import com.ma.common.result.Result;
import com.ma.common.utils.MD5;
import com.ma.model.system.SysUser;
import com.ma.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/sysUser")
@Api(tags = "用户管理接口")
public class SysUserController {
    @Autowired
    private SysUserService userService;

    //用户的条件分页查询
    @ApiOperation("用户的条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result<Page<SysUser>> index(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo){
        Page<SysUser> userPage = userService.limit(page, limit, sysUserQueryVo);
        return Result.ok(userPage);
    }
    //用户的新增
    @ApiOperation("用户的新增")
    @PostMapping("save")
    public Result save(@RequestBody SysUser sysUser){
        //对密码进行加密处理，使用MD5加密方式
        String passwordMD5 = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(passwordMD5);
        boolean isSuccess = userService.save(sysUser);
        if(isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //获取用户
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = userService.getById(id);
        return Result.ok(user);
    }
    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        boolean isSuccess = userService.updateById(user);
        if(isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = userService.removeById(id);
        if(isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("批量删除")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> ids){
        boolean isSuccess = userService.removeByIds(ids);
        if(isSuccess){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("更新用户状态")
    @PutMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        Boolean isSuccess = userService.updateStatus(id, status);
        if(isSuccess){
            return Result.ok();
        }else{
            return Result.fail().message("状态更新失败");
        }
    }
}
